package com.dwjf.core.activities;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ContentFrameLayout;

import com.dwjf.core.R;
import com.dwjf.core.delegates.LatteDelegate;

import me.yokeyword.fragmentation.ExtraTransaction;
import me.yokeyword.fragmentation.ISupportActivity;
import me.yokeyword.fragmentation.SupportActivityDelegate;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

public abstract class ProxyActivity extends AppCompatActivity implements ISupportActivity {

    private final SupportActivityDelegate delegate = new SupportActivityDelegate(this);

    public abstract LatteDelegate setRootDelegare();



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        delegate.onCreate(savedInstanceState);
        initContainer(savedInstanceState);


    }

    @SuppressLint("RestrictedApi")
    private void initContainer(@Nullable Bundle savedInstanceState){
        //Context context = Latte.getApplication();
        final ContentFrameLayout container = new ContentFrameLayout(this);
        container.setId(R.id.delegate_container);
        setContentView(container);
        if (savedInstanceState == null){
            delegate.loadRootFragment(R.id.delegate_container,setRootDelegare());
        }
    }

    @Override
    protected void onDestroy() {
        delegate.onDestroy();
        super.onDestroy();
        System.gc();
        System.runFinalization();
    }


    @Override
    public SupportActivityDelegate getSupportDelegate() {
        return delegate;
    }

    @Override
    public ExtraTransaction extraTransaction() {
        return delegate.extraTransaction();
    }

    @Override
    public FragmentAnimator getFragmentAnimator() {
        return delegate.getFragmentAnimator();
    }

    @Override
    public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {
        delegate.setFragmentAnimator(fragmentAnimator);
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return delegate.onCreateFragmentAnimator();
    }

    @Override
    public void onBackPressedSupport() {
        delegate.onBackPressedSupport();
    }

    @Override
    public void onBackPressed() {
        delegate.onBackPressed();
    }
}
